{% extends "base.html" %}

{% block head %}
    <title>上传题目</title>
    <link rel="stylesheet" href="/static/css/uploadTopic.css">
{% endblock %}

{% block content %}
    <div class="container">
        <h1>试题上传系统</h1>
        <form id="questionForm" method="POST">
            <label for="title">题目标题:</label>
            <input type="text" id="title" name="title" value="{{ question.title if question else '' }}" required>

            <label for="options-container">题目类型:</label>
            <div class="options-container">
                {% for type in ['解答题', '选择题', '判断题', '填空题', '其他'] %}
                    <label>
                        <input type="radio" name="type" value="{{ type }}"
                        {% if question and type == question.type %}checked{% endif %}> {{ type }}
                    </label>
                {% endfor %}
            </div>

            <div id="optionsContainer" class="{% if question and question.type != '选择题' %}hidden{% endif %}">
                <label>选项:</label>
                <div id="optionInputs">
                    {% if question %}
                        {% for option in question.options %}
                            <input type="text" class="option" name="options" value="{{ option }}" placeholder="选项:">
                        {% endfor %}
                    {% else %}
                        <input type="text" class="option" name="options" placeholder="A:">
                        <input type="text" class="option" name="options" placeholder="B:">
                        <input type="text" class="option" name="options" placeholder="C:">
                        <input type="text" class="option" name="options" placeholder="D:">
                    {% endif %}
                </div>
                <button type="button" onclick="addOption()">添加选项</button>
            </div>

            <label for="content">题目内容:</label>
            <textarea id="content" name="content" rows="4">{{ question.content if question else '' }}</textarea>

            <label for="answer">正确答案:</label>
            <input type="text" id="answer" name="answer" value="{{ question.answer if question else '' }}" required>

            <label for="difficulty">难度:</label>
            <select id="difficulty" name="difficulty">
                <option value="简单" {% if question and question.difficulty == '简单' %}selected{% endif %}>简单</option>
                <option value="中等" {% if question and question.difficulty == '中等' %}selected{% endif %}>中等</option>
                <option value="困难" {% if question and question.difficulty == '困难' %}selected{% endif %}>困难</option>
            </select>

            <label for="explanation">解析:</label>
            <textarea id="explanation" name="explanation" rows="3">{{ question.explanation if question else '' }}</textarea>

            <button type="submit" class="submit-btn">{% if isEdit %}更改{% else %}提交{% endif %}</button>
        </form>
    </div>
{% endblock %}

{% block js %}
    <script>
        const isEdit = {{ isEdit }};
        {% if isEdit %} const questionId = {{ question.id }};{% endif %}
    </script>
    <script src="/static/js/uploadTopic.js" type="module"></script>
{% endblock %}
